package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
	private IRoleService service;
    @Autowired
	private IPermissionService permissionService;
	@RequestMapping("/list")
	@RequiresPermissions(value = {"角色查询","role:list"},logical = Logical.OR)
	public String list(Model model, @ModelAttribute("qo")QueryObject qo){
		PageInfo result = service.query(qo);
		model.addAttribute("result", result);
		return "role/list";
	}
	@RequestMapping("/input")
	@RequiresPermissions(value = {"角色编辑","role:input"},logical = Logical.OR)
	public String input(Model model,Long id){
		if(id!=null){
			Role entity = service.selectOne(id);
			model.addAttribute("entity", entity);

		}
			List<Permission> permissions = permissionService.listAll();
			model.addAttribute("permissions", permissions);
		return "role/input";
	}
	@RequestMapping("/saveOrUpdate")
	@RequiresPermissions(value = {"角色更新","role:saveOrUpdate"},logical = Logical.OR)
	public String list(Model model,Role u,int [] ids){
		service.saveOrUpdate(u,ids);
		return "redirect:/role/list.do";
	}
	@RequestMapping("/delete")
	@RequiresPermissions(value = {"角色删除","role:delete"},logical = Logical.OR)
	public String list(Long id){
		service.delete(id);
		return "redirect:/role/list.do";
	}
}
